Hrana client for TypeScript
API docs | Github | npm
This package implements a Hrana client for TypeScript. Hrana is the protocol for connecting to sqld using WebSocket or HTTP.
This package is intended mostly for internal use. Consider using the @libsql/client
package, which will use Hrana automatically.
Usage
import * as hrana from "@libsql/hrana-client";
const url = process.env.URL ?? "ws://localhost:8080";
const jwt = process.env.JWT;
const client = hrana.openWs(url, jwt);
const stream = client.openStream();
const books = await stream.query("SELECT title, year FROM book WHERE author = 'Jane Austen'");
for (const book of books.rows) {
console.log(`${book.title} from ${book.year}`);
}
const book = await stream.queryRow("SELECT title, MIN(year) FROM book");
if (book.row !== undefined) {
console.log(`The oldest book is ${book.row.title} from year ${book.row[1]}`);
}
const year = await stream.queryValue(["SELECT MAX(year) FROM book WHERE author = ?", ["Jane Austen"]]);
if (year.value !== undefined) {
console.log(`Last book from Jane Austen was published in ${year.value}`);
}
const res = await stream.run(["DELETE FROM book WHERE author = ?", ["J. K. Rowling"]])
console.log(`${res.affectedRowCount} books have been cancelled`);
client.close();